//
//  UIBarButtonItem+CommomBarButtonItem.m
//  ZYGWApp
//
//  Created by More on 14-7-9.
//  Copyright (c) 2014年 ZYGW. All rights reserved.
//

#import "UIBarButtonItem+CommomBarButtonItem.h"

@implementation UIBarButtonItem (CommomBarButtonItem)

+ (UIBarButtonItem*)createBackBBTframe:(CGRect)frame isShowArrow:(BOOL)show isShowTitle:(BOOL)showTitle target:(id)target action:(SEL)action
{
    //
    UIImage * leftImage = [UIImage imageNamed:@"icon_nav_menu"];
//    if(show)
//    {
//        NSString *path = [[NSBundle mainBundle] pathForResource:@"icon_nav_menu" ofType:@"png"];
//        leftImage = [UIImage imageWithContentsOfFile:path];
//    }
    
    // 按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setBackgroundColor:[UIColor clearColor]];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    btn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:leftImage forState:UIControlStateNormal];
    
    if(showTitle)
    {
        [btn setTitle:@"返回" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
    
    CGFloat edgeRight = btn.frame.size.width-leftImage.size.width;
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, edgeRight)];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(1, 0, 0,30)];

    }
    else
    {
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -20, 0, edgeRight-20)];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 1,15)];

    }

    return [[UIBarButtonItem alloc] initWithCustomView:btn];
    
}

+ (UIBarButtonItem*)createDefaultTitleBBTFrame:(CGRect)frame title:(NSString*)title EdgeInsets:(UIEdgeInsets)edgeInsets font:(UIFont*)font background:(UIColor*)bgColor target:(id)target action:(SEL)action
{
    UIButton *cBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    // frame
    cBtn.frame = frame;
    
    // 标题
    [cBtn setTitle:title forState:UIControlStateNormal];
    
    // 边距
    [cBtn setTitleEdgeInsets:edgeInsets];

    // 字体
    if(!font)
    {
        cBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    else
    {
        cBtn.titleLabel.font = font;
    }

    // 背景色
    if(bgColor)
    {
        cBtn.backgroundColor = bgColor;
    }
    // 事件
    [cBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    
    return [[UIBarButtonItem alloc] initWithCustomView:cBtn];
}

+ (UIBarButtonItem*)createDefaultImageBBTFrame:(CGRect)frame image:(UIImage*)image background:(UIColor*)bgColor EdgeInsets:(UIEdgeInsets)edgeInsets target:(id)target action:(SEL)action
{
    UIButton *cBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    // frame
    cBtn.frame = frame;
    
    // 标题
    [cBtn setImage:image forState:UIControlStateNormal];
    [cBtn.imageView setContentMode:UIViewContentModeCenter];
    
    // 图片边距
    [cBtn setImageEdgeInsets:edgeInsets];
    
    // 背景色
    if(bgColor)
    {
        cBtn.backgroundColor = bgColor;
    }
    // 事件
    [cBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    
    return [[UIBarButtonItem alloc] initWithCustomView:cBtn];
}

+ (UIBarButtonItem*)createImageAndTitleBBTFrame:(CGRect)frame image:(UIImage*)image background:(UIColor*)bgColor imgEdgeInsets:(UIEdgeInsets)imgedgeInsets title:(NSString*)title titleEdgeInsets:(UIEdgeInsets)titledgeInsets titlefont:(UIFont*)titlefont target:(id)target action:(SEL)action
{
    UIButton *cBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    // frame
    cBtn.frame = frame;
    
    // 图片
    [cBtn setImage:image forState:UIControlStateNormal];
    [cBtn.imageView setContentMode:UIViewContentModeCenter];
    // 文字
    [cBtn setTitle:title forState:UIControlStateNormal];
    [cBtn.titleLabel setContentMode:UIViewContentModeCenter];
    // 字体
    if(!titlefont)
    {
        cBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    else
    {
        cBtn.titleLabel.font = titlefont;
    }

    // 图片边距
    [cBtn setImageEdgeInsets:imgedgeInsets];
    
    // 背景色
    if(bgColor)
    {
        cBtn.backgroundColor = bgColor;
    }
    // 事件
    [cBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc] initWithCustomView:cBtn];
}
+ (UIBarButtonItem*)createRightImgBBTframe:(CGRect)frame imgName:(NSString*)imgName isShowArrow:(BOOL)show isShowTitle:(BOOL)showTitle target:(id)target action:(SEL)action
{
    //
    UIImage * leftImage = [UIImage imageNamed:imgName];
    //    if(show)
    //    {
    //        NSString *path = [[NSBundle mainBundle] pathForResource:@"icon_nav_menu" ofType:@"png"];
    //        leftImage = [UIImage imageWithContentsOfFile:path];
    //    }
    
    // 按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setBackgroundColor:[UIColor clearColor]];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    btn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:leftImage forState:UIControlStateNormal];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    
    if(showTitle)
    {
        [btn setTitle:@"添加" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
    
    CGFloat edgeRight = btn.frame.size.width-leftImage.size.width;
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, edgeRight-55)];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(1, 0, 0,30)];
        
    }
    else
    {
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, edgeRight-40)];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 1,15)];
        
    }
    
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
    
}

+ (UIBarButtonItem*)createLeftImgBBTframe:(CGRect)frame imgName:(NSString*)imgName isShowArrow:(BOOL)show isShowTitle:(BOOL)showTitle target:(id)target action:(SEL)action
{
    //
    UIImage * leftImage = [UIImage imageNamed:imgName];
    //    if(show)
    //    {
    //        NSString *path = [[NSBundle mainBundle] pathForResource:@"icon_nav_menu" ofType:@"png"];
    //        leftImage = [UIImage imageWithContentsOfFile:path];
    //    }
    
    // 按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setBackgroundColor:[UIColor clearColor]];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    btn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:leftImage forState:UIControlStateNormal];
    [btn.imageView setContentMode:UIViewContentModeCenter];
    
    if(showTitle)
    {
        [btn setTitle:@"返回" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
    
    CGFloat edgeRight = btn.frame.size.width-leftImage.size.width;
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, edgeRight)];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(1, 0, 0,30)];
        
    }
    else
    {
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -20, 0, edgeRight-20)];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 1,15)];
        
    }
    
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
    

}




@end
